#include "mypeoplewidget.h"
#include<qDebug>
#include<QPushButton>
#include<QLineEdit>
#include<QHBoxLayout>
#include<QLabel>
#include<QFile>
#include<QApplication>
#include<QDate>
#include<QByteArray>
#include<QDir>
#include<QTimer>
#include<QStandardPaths>
#include<QLibrary>
#include<QThread>
#define MYLOG qDebug() << "[" << __FILE__ << ":" << __LINE__ << ":" << QThread::currentThread() << __func__ << QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss:zzz")<< "]"
MyPeopleWidget::MyPeopleWidget(QWidget *parent) :
  QWidget(parent,Qt::WindowStaysOnTopHint)
{

    readText();

  create();

  m_date = QDate::currentDate();
  currentDate = m_date.toString("yyyy-MM-dd");

  connect(btn,SIGNAL(clicked()),this,SLOT(btn_clicked()));

emit mySignalZuHao(zuhao);

}

MyPeopleWidget::~MyPeopleWidget()
{
delete btn;
  btn = nullptr;
delete lineEdit;
  lineEdit = nullptr;
delete labelTip;
  labelTip = nullptr;
}


void MyPeopleWidget::create()
{
  //  setFixedSize(900,100);
  setWindowTitle("人员分组");
  QLabel *tip = new QLabel("上次分组:",this);

  tip->setFixedSize(150,60);
  tip->setFont(QFont(QString::asprintf("Cambria Math"), 18, QFont::Black));

  labelTip = new QLabel("0",this);
  labelTip->setFixedSize(80,60);
  labelTip->setFont(QFont(QString::asprintf("Cambria Math"), 18, QFont::Black));

  btn = new QPushButton("分组",this);
  btn->setFixedSize(100,60);

  btn->setFont(QFont(QString::asprintf("Cambria Math"), 18, QFont::Black));

  lineEdit = new QLineEdit("1",this);
  lineEdit->setFixedSize(80,60);
  lineEdit->setFont(QFont(QString::asprintf("Cambria Math"), 18, QFont::Black));

  //弹簧
  QSpacerItem *h_spacer = new QSpacerItem(10,10,QSizePolicy::Expanding,QSizePolicy::Minimum);

  QHBoxLayout *layout = new QHBoxLayout(this);
  layout->addItem(h_spacer);
  layout->addWidget(tip);
  layout->addItem(h_spacer);
  layout->addWidget(labelTip);
  layout->addItem(h_spacer);
  layout->addWidget(lineEdit);
  layout->addItem(h_spacer);
  layout->addWidget(btn);
  layout->addItem(h_spacer);


  setLayout(layout);

  if(BeforeGroupNumber != -1){
      zuhao = QString::number(BeforeGroupNumber);
      labelTip->setText(zuhao);
      BeforeGroupNumber += 1;
      lineEdit->setText(QString::number(BeforeGroupNumber));
    }else{
      zuhao = QString::number(CurrentGroupNumber);
      labelTip->setText(zuhao);

      CurrentGroupNumber += 1;
      lineEdit->setText(QString::number(CurrentGroupNumber));
    }
}


void MyPeopleWidget::readText()
{

    QString  strm = QApplication::applicationDirPath();
    strm += "/config2.txt";

    QFile file(strm);

    if(!file.open(QIODevice::ReadWrite))
      qDebug()<<"Can't open the file!";

    QByteArray line = file.readLine();
    if(line.size() > 0)
      {
        QString str = line;
        str.replace("\n","");

        QString s1,s2;
        for(int i = 0;i < 10;i++)
          {
            s1.append(str[i]);
          }
        for(int i = 11;i < str.size();i++)
          {
            s2.append(str[i]);
          }

        if(s1 == currentDate)
          {
            zuhao = s2;

            BeforeGroupNumber = zuhao.toInt();
          }
        else{
            zuhao = QString::number(CurrentGroupNumber);

          }




      }


    file.close();

}

void MyPeopleWidget::writeText()
{

    m_date = QDate::currentDate();
    currentDate = m_date.toString("yyyy-MM-dd");

    QString  strm = QApplication::applicationDirPath();

    strm += "/config2.txt";
    QDir path; // 创建一个QDir变量
    if (!path.exists(strm)) {  // 使用QDir成员函数exists()来判断文件夹是否存在
        path.mkdir(strm);  // 使用mkdir来创建文件夹
      }

    strm.replace("/","\\");

    QFile file(strm);

    if(!file.open(QIODevice::ReadWrite))
      qDebug()<<"Can't open the file!";



    currentDate += "-"+zuhao;

    file.write(currentDate.toUtf8());
    file.close();


}


void MyPeopleWidget::btn_clicked()
{


  if(BeforeGroupNumber != -1){
      zuhao = QString::number(BeforeGroupNumber);
      labelTip->setText(zuhao);
      BeforeGroupNumber += 1;
      lineEdit->setText(QString::number(BeforeGroupNumber));
    }else{
      zuhao = QString::number(CurrentGroupNumber);
      labelTip->setText(zuhao);

      CurrentGroupNumber += 1;
      lineEdit->setText(QString::number(CurrentGroupNumber));
    }

  emit mySignalZuHao(zuhao);
  writeText();

  QMessageBox::information(this,tr("提示"),tr("分组成功!"),QMessageBox::Ok);

  this->close();
}



